package com.arrake.pojo;

import com.arrake.utils.DocxReader;

import java.io.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class DocxPicture {
    private String pathPicture;
    //    格式:height:14.95pt;width:40.75pt;
    private String style;
    //    自定义
    private String id;
    private String docxString;
    private String relationshipString;

    public DocxPicture(String pathPicture, String style, String id) throws Exception {
        docxString = "<w:r>\n" +
                "                <w:object>\n" +
                "                    <v:shape  style=\"" + style + "\" >\n" +
                "                \n" +
                "                        <v:imagedata r:id=\"" + id + "\"/>\n" +
                "                    </v:shape>\n" +
                "                </w:object>\n" +
                "            </w:r>\n";

//        将pathPicture下的图片复制到media文件夹下，并要确保id唯一
        String pathname = copyPicture(pathPicture);
        relationshipString = "<Relationship Id=\"" + id + "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/" + pathname + "\"/>\n";

        RelationShip instanceRelationShip = RelationShip.getInstanceRelationShip();
        instanceRelationShip.AddNode(relationshipString);
    }

    //    选择这个构造方法图片将是从压缩图片来的
    public DocxPicture(String pathPicture, String style, String id, String filePath) throws Exception {
        docxString = "<w:r>\n" +
                "                <w:object>\n" +
                "                    <v:shape  style=\"" + style + "\" >\n" +
                "                \n" +
                "                        <v:imagedata r:id=\"" + id + "\"/>\n" +
                "                    </v:shape>\n" +
                "                </w:object>\n" +
                "            </w:r>\n";

//        将pathPicture下的图片复制到media文件夹下，并要确保id唯一
        String pathname = copyPicture(pathPicture, filePath);
        relationshipString = "<Relationship Id=\"" + id + "\" Type=\"http://schemas.openxmlformats.org/officeDocument/2006/relationships/image\" Target=\"media/" + pathname + "\"/>\n";

        RelationShip instanceRelationShip = RelationShip.getInstanceRelationShip();
        instanceRelationShip.AddNode(relationshipString);
    }

    private String copyPicture(String pathPicture) throws Exception {
        String dir = "src\\main\\resources\\docxTemplate\\word\\media";
        String fileName = null;
        String sufFileName = null;

//        获取文件名后缀
        Matcher matcher = Pattern.compile("\\..*").matcher(pathPicture);
        matcher.find();
        sufFileName = matcher.group();

//        遍历文件获取文件名
        File file = new File(dir);
        File[] files = file.listFiles();
        for (File file1 : files) {
            String[] split1 = file1.getName().split("\\.");
            String num1 = split1[0].substring(5);
            String[] split2 = fileName.split("\\.");
            String num2 = split2[0].substring(5);
            if (Integer.parseInt(num1) > Integer.parseInt(num2)) {
                fileName = "image" + Integer.parseInt(num1) + sufFileName;
            }
        }

        if (fileName == null) {
            fileName = "image1" + sufFileName;
        }


        InputStream in = new FileInputStream(new File(pathPicture));
        OutputStream out = new FileOutputStream(new File(dir + "//" + fileName));

        byte[] buffer = new byte[1024];
        int len;

        while ((len = in.read(buffer)) > 0) {
            out.write(buffer, 0, len);
        }
        in.close();
        out.close();
        return fileName;

    }

    private String copyPicture(String pathPicture, String filepath) throws Exception {
        return DocxReader.zipPictureCopy(pathPicture, filepath);
    }

    public String getPathPicture() {
        return pathPicture;
    }

    public String getStyle() {
        return style;
    }

    public String getId() {
        return id;
    }

    public String getDocxString() {
        return docxString;
    }


    @Override
    public String toString() {
        return "DocxPicture{" +
                "pathPicture='" + pathPicture + '\'' +
                ", style='" + style + '\'' +
                ", id='" + id + '\'' +
                ", docxString='" + docxString + '\'' +
                ", relationshipString='" + relationshipString + '\'' +
                '}';
    }
}
